'''
Author: aggfb 714740235@qq.com
Date: 2024-10-23 07:17:00
LastEditors: aggfb 714740235@qq.com
LastEditTime: 2024-10-26 12:57:01
FilePath: \pythonProject1\serialer.py
Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
'''
import serial
import time
import threading

# 串口实例
global ser_send_array
global ser_recv_array

# 创建读串口数据线程
def creat_task(ser):
    # 串口通信线程
    print("1.创建串口发送线程")
    thread_send = threading.Thread(target=serial_thread_send, args=[ser])
    thread_send.daemon = True

    print("2.创建串口接收线程")
    thread_recv = threading.Thread(target=serial_thread_recv, args=[ser])
    thread_recv.daemon = True
    
    thread_send.start()
    thread_recv.start()

def serial_thread_send(ser):
    while True:
        unicode_string = 'cjson\r\n'
        data_string = unicode_string.encode('utf-8')
        ser.write(data_string)
        time.sleep(1)

def serial_thread_recv(ser):
    while True:
        data = ser.readline()
        print("recv = "+data.decode('utf-8'))


